package com.dongjiantong.web.boss.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.utils.StringUtil;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.user.model.LoginUser;
import com.dongjiantong.user.model.MzUser;
import com.dongjiantong.user.enums.UserEnum;
import com.dongjiantong.user.model.dtos.BossUserDto;
import com.dongjiantong.user.service.*;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * boss平台人员查看，包括政府人员，养殖客户人员，运维人员
 * @author cao
 * @create 2017-11-15 下午6:21
 **/
//@RequestMapping("/api/boss")
//@RestController
@Slf4j
@OpenBean
public class MzUserController {

    @Reference(version = "1.0.0")
    public BossUserService bossUserService;

    @Reference(version = "1.0.1")
    public OrgUserService orgUserService;

    @Reference(version = "1.0.0")
    public FarmsUserService farmsUserService;

    @Reference(version = "1.0.0")
    public MzUserService mzUserService;

    @Reference(version = "1.0.0")
    public SSOLoginService ssoLoginService;





    /**
     * 运维人员列表
     * @param searchName
     * @param pageNum
     * @param pageSize
     * @return
     */
    //@GetMapping("/bossUserList")
    @OpenMethod(
            key = "boss.bossuser.list",
            paramNames = {"searchName", "pageNum", "pageSize"},
            httpMethods = RequestMethod.GET
    )
    public Response<PageInfo<BossUserDto>> getBossUserList(String searchName, Integer pageNum, Integer pageSize)
    {

        if(pageNum==null)
        {
            pageNum=1;
        }
        if(pageSize==null)
        {
            pageSize=10;
        }
        Map<String,Object> searchMap = new HashMap<String, Object>();
        if(!StringUtil.isBlank(searchName)) {
            searchMap.put("realname", searchName);
        }
        return Response.ok(bossUserService.getBossListByPage(UserEnum.USERTYPE_BOSS, pageNum,  pageSize,searchMap));
    }

    /**
     * 创建运维人员
     * @return
     */
    //@PostMapping("/bossCreateUser")
    @OpenMethod(
            key = "boss.create.bossuser",
            paramNames = {"mzUser", "password"},
            httpMethods = RequestMethod.POST
    )
    public Response<Boolean> createUser(MzUser mzUser,String password)
    {

        //验证登录手机号是否存在
        String mobile = mzUser.getMobile();

        if(StringUtil.isBlank(mobile))
        {
            return Response.fail(HttpCode.BAD_REQUEST,"手机号码不能为空");
        }
        if(StringUtil.isBlank(password))
        {
            return Response.fail(HttpCode.BAD_REQUEST,"密码不能为空");
        }
        else
        {
            if(password.length()<6)
            {
                return Response.fail(HttpCode.BAD_REQUEST,"密码长度不足，推荐长度为6-10位");
            }
        }

       Response<Boolean> response =  ssoLoginService.checkUserName(mobile);

       if(response.getResult())
       {
           return Response.fail(HttpCode.BAD_REQUEST,"登录帐号已经存在");
       }


        //基本信息

        mzUser.setUserType(UserEnum.USERTYPE_BOSS.getCode());
        mzUser.setIsmaster(0);
        mzUser.setDeleteFlag(1);
        mzUser.setRelationId(0L);
        mzUser.setCreatedId(1L);
        mzUser.setUpdatedId(1L);
        mzUser.setCreatedAt(new Date());
        mzUser.setUpdatedAt(new Date());

        //登录信息
        LoginUser loginUser = new LoginUser();
        loginUser.setPassword(password);
        loginUser.setUsername(mobile);

        boolean result = bossUserService.createBossUsers(mzUser,loginUser);
        return Response.ok(result);
    }

    //@GetMapping("/bossUser")
    @OpenMethod(
            key = "boss.bossuser.info",
            paramNames = {"userId"},
            httpMethods = RequestMethod.GET
    )
    public Response<MzUser> queryBossUser(long userId)
    {

        MzUser mzUser =   mzUserService.getMzUser(userId);

        if(mzUser.getUserType().equals( UserEnum.USERTYPE_BOSS.getCode())) {
            return Response.ok(mzUser);
        }
        else
        {
            return Response.fail(HttpCode.BAD_REQUEST,"获取运维人员类型不符。");
        }
    }

    /**
     * 保存编辑运维人员信息
     * @param mzUser
     * @param password
     * @return
     */
    //@PostMapping("/bossEditUser")
    @OpenMethod(
            key = "boss.bossuser.edit",
            paramNames = {"mzUser","password"},
            httpMethods = RequestMethod.POST
    )
    public Response<Boolean> editUser(MzUser mzUser,String password)
    {
        //只能修改职位，身份证号，密码

        if(mzUser.getId()<=0){
            return Response.fail(HttpCode.BAD_REQUEST,"信息不正确，不能提交");
        }
        mzUser.setUpdatedId(1L);
        mzUser.setUpdatedAt(new Date());
        //登录信息
        LoginUser loginUser = new LoginUser();

        loginUser.setUserId(mzUser.getId());
        if(!StringUtil.isBlank(password)) {
            loginUser.setPassword(password);
        }
        boolean result = bossUserService.editBossUsers(mzUser,loginUser);
        return Response.ok(result);
    }

    /**
     * 禁用人员帐号(运维，政府，企业通用)
     * @param userId
     * @return
     */
    //@PostMapping("/DisableUser")
    @OpenMethod(
            key = "boss.disable.user",
            paramNames = {"userId"},
            httpMethods = RequestMethod.POST
    )
    public Response<Boolean> disableUser(Long userId)
    {
        boolean result = ssoLoginService.updateUserLoginStatus(userId,UserEnum.LOGIN_ENABLE);
        return Response.ok(result);
    }

    /**
     * 启用人员帐号(运维，政府，企业通用)
     * @param userId
     * @return
     */
    //@PostMapping("/EnableUser")
    @OpenMethod(
            key = "boss.enable.user",
            paramNames = {"userId"},
            httpMethods = RequestMethod.POST
    )
    public Response<Boolean> enableUser(Long userId)
    {
        boolean result = ssoLoginService.updateUserLoginStatus(userId,UserEnum.LOGIN_DISABLE);
        return Response.ok(result);
    }





}
